/**
 * 
 */
package com.cqan.exception;

import java.text.MessageFormat;
import java.util.ResourceBundle;

/**
 * @Description TODO
 * @author wuhui
 * @date 2012-4-27下午5:37:58
 *
 */
@SuppressWarnings("serial")
public class AppException extends Exception {
	
	private static final ResourceBundle rb = ResourceBundle.getBundle("i18n/exception-message");
	
	 //默认的messageKey  
    public static final String DEFAULT_MESSAGE_KEY = "message";
    
    private String messageKey = DEFAULT_MESSAGE_KEY;  
	
    //用于格式化输出message的数据  
    private Object[] args;  
    
    public AppException() {  
    }
    
    public AppException(String message) {  
        this.messageKey = message;  
    }  
    public AppException(String messageKey, Object[] args) {  
        this.messageKey = messageKey;  
        this.args = args;  
    }  
    public AppException(String messageKey, Object[] args, Throwable cause) {  
        super(cause);  
        this.messageKey = messageKey;  
        this.args = args;  
    }  
	
	@Override  
    public String getLocalizedMessage() {  
        String text = rb.getString(messageKey);  
        if (null == text || "".equals(text)){  
            return rb.getString(DEFAULT_MESSAGE_KEY);
        }
        MessageFormat mf = new MessageFormat(text);  
        return mf.format(args);  
    }

}
